package com.nhim.cms.admin.client;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.logical.shared.OpenEvent;
import com.google.gwt.event.logical.shared.OpenHandler;
import com.google.gwt.event.logical.shared.SelectionEvent;
import com.google.gwt.event.logical.shared.SelectionHandler;
import com.google.gwt.user.client.Command;
import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.Event;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.MenuBar;
import com.google.gwt.user.client.ui.PopupPanel;
import com.google.gwt.user.client.ui.TreeItem;
import com.nhim.cms.admin.client.ExtendedTree.Listener;
import com.nhim.cms.admin.client.contentmanagement.ui.ContentTypeSelect;
import com.nhim.cms.admin.shared.SimpleTreeItem;
import com.nhim.cms.admin.shared.ContentTypeRPCProxy;
import com.nhim.cms.admin.shared.NhimCMSAdminRequestFactory;

public class ContentPagesTree extends Composite {
	private ExtendedTree tree;
	private TreeItem priviousSelectedItem;
	private TreeItem selectedItem;
	final private PopupPanel popupPanel = new PopupPanel(true);

	public ContentPagesTree(NhimCMSAdminRequestFactory requestFactory) {

		createPopupMenu();
		tree = createContentTree();
		initWidget(tree);
		tree.addRightClickListener(new Listener() {

			@Override
			public void onRightClick(TreeItem treeItem, Event event) {
				selectedItem = treeItem;
				showContextMenu(event);
				
			}
		});
	}

	private void showContextMenu(Event event) {
		int x = DOM.eventGetClientX(event);
		int y = DOM.eventGetClientY(event);
		popupPanel.setPopupPosition(x, y);
		popupPanel.show();
	}
	private int getDateTime() {
        Date date = new Date();
        int iTimeStamp = (int) (date.getTime() * .001);
        return iTimeStamp;
}

	Command createNewPageCommand = new Command() {
		public void execute() {
			popupPanel.hide();
			ContentTypeSelect select = new ContentTypeSelect();
			select.setGlassEnabled(true);
			select.center();
			select.setListener(new ContentTypeSelect.Listener() {
				
				@Override
				public void onContentTypeSelected(ContentTypeRPCProxy proxy) {
					//create new child item
					SimpleTreeItem simpleTreeItem = new SimpleTreeItem("Hello" + getDateTime() );
					simpleTreeItem.setId(getDateTime());
					TreeItem newItem = new TreeItem(simpleTreeItem.getName());
					newItem.setUserObject(simpleTreeItem);
					selectedItem.addItem(newItem);
					selectedItem.setState(true,true);
					newItem.setSelected(true);
					if(priviousSelectedItem != null)
						priviousSelectedItem.setSelected(false);				
					priviousSelectedItem = newItem;
					if(selectionChangeListener != null)
						selectionChangeListener.onTreeItemSelected(simpleTreeItem);
				}
			});
		}
	};

	Command deleteCommand = new Command() {
		public void execute() {
			popupPanel.hide();
			
		}
	};

	private void createPopupMenu() {

		MenuBar popupMenuBar = new MenuBar(true);
		com.google.gwt.user.client.ui.MenuItem alertItem = new com.google.gwt.user.client.ui.MenuItem(
				"Create New Page", true, createNewPageCommand);
		com.google.gwt.user.client.ui.MenuItem imageItem = new com.google.gwt.user.client.ui.MenuItem(
				"Delete", true, deleteCommand);

		popupPanel.setStyleName("popup");
		alertItem.addStyleName("popup-item");
		imageItem.addStyleName("popup-item");

		popupMenuBar.addItem(alertItem);
		popupMenuBar.addItem(imageItem);

		popupMenuBar.setVisible(true);
		popupPanel.add(popupMenuBar);
	}

	private ExtendedTree createContentTree() {
		tree = new ExtendedTree();
		tree.setAnimationEnabled(true);
		TreeItem tItem = new TreeItem("WebSite");
		SimpleTreeItem root = new SimpleTreeItem("WebSite");
		root.setId(0);
		tItem.setUserObject(root);
		
		tree.addOpenHandler(getOpenHandler());
		tree.addSelectionHandler(getSelectionHandler());
		tree.addItem(tItem);
		tree.setSelectedItem(tItem, true);
		return tree;
	}

	private OpenHandler<TreeItem> getOpenHandler() {
		return new OpenHandler<TreeItem>() {
			@Override
			public void onOpen(OpenEvent<TreeItem> oe) {
				GWT.log("Tree Item openned");
				TreeItem targetItem = (TreeItem) oe.getTarget();
				// stupid but would be usefull.
				if (targetItem.getChildCount() > 1) {
					return;
				}
				//fetchTreeItems(targetItem);
			}
		};
	}

	private SelectionHandler<TreeItem> getSelectionHandler() {
		return new SelectionHandler<TreeItem>() {
			@Override
			public void onSelection(SelectionEvent<TreeItem> se) {
				TreeItem currentTreeItem = (TreeItem) se.getSelectedItem();
				SimpleTreeItem currentTreeObject = (SimpleTreeItem) currentTreeItem.getUserObject();
				if( currentTreeObject.getId() > 0)
				{
					if(priviousSelectedItem != null)
						priviousSelectedItem.setSelected(false);
					priviousSelectedItem = currentTreeItem;
					selectionChangeListener.onTreeItemSelected(currentTreeObject);
				}else
					currentTreeItem.setSelected(false);

			}
		};
	}

	private void fetchTreeItems(final TreeItem father) {
		List<SimpleTreeItem> result = new ArrayList<SimpleTreeItem>();
		for (int i = 0; i < 2; i++) {
			SimpleTreeItem item = new SimpleTreeItem(father.getText() + "."
					+ String.valueOf(i));
			result.add(item);
		}
		expandTreeItem(father, result);

	}

	private void expandTreeItem(TreeItem father, List<SimpleTreeItem> simpleTreeItems) {
		father.removeItems();
		for (SimpleTreeItem simpleTreeItem : simpleTreeItems) {

			TreeItem newItem = new TreeItem(simpleTreeItem.getName());
			newItem.setUserObject(simpleTreeItem);
			father.addItem(newItem);
		}
	}
	private SelectionChangeListener selectionChangeListener;

	/**
	 * Custom listener for this widget.
	 */
	public interface SelectionChangeListener {

		/**
		 * Called when the user save Template
		 * 
		 */
		void onTreeItemSelected(SimpleTreeItem seletedItem);
	}

	public void onSelectionChange(SelectionChangeListener listener) {
		this.selectionChangeListener = listener;
	}

	

}
